home *** CD-ROM | disk | FTP | other *** search
/ PC-X 1997 October / pcx14_9710.iso / swag / delphi.swg / 0157_Execute & Wait for Delphi 2.0.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-08-30  |  5.3 KB  |  193 lines

  1.  
  2. {Here's an implementation (based on Pat's WinExecAndWait32) that uses a thread, so that your application can continue to process windows messages (and do something else) while it waits for the launched application to finish.}
  3.  
  4. unit WinExec;
  5. interface
  6. uses
  7.   Classes, SysUtils, Windows;
  8. type
  9.   TWinExec = class(TThread)
  10.   private
  11.     FFileName, FArguments: String;
  12.     FProcessActive: boolean;
  13.     FOnProcessIdle: TNotifyEvent;
  14.   public
  15.     constructor Create(AppName, Args : String; OnExit, OnIdle : TNotifyEvent);
  16.   protected
  17.     procedure Execute; override;
  18.     function StartApp(AppName, ArgStr :String; Visibility : integer):integer;
  19.   end;
  20. type
  21.   FileSpec = array [0..MAX_PATH] of char;
  22. implementation
  23. { TWinExec }
  24. constructor TWinExec.Create(AppName, Args: String; OnExit, OnIdle: TNotifyEvent);
  25. begin
  26.   inherited Create(False);
  27.   FOnProcessIdle := OnIdle;
  28.   FFileName := AppName;
  29.   FArguments := Args;
  30.   FProcessActive := False;
  31.   FreeOnTerminate := False;  // Note that this implementation requires you 
  32.   OnTerminate := OnExit;     // to explicitly Free the TWinExec object.    
  33. end;
  34. procedure TWinExec.Execute;
  35. begin
  36.   FProcessActive := True;
  37.   StartApp(FFileName, FArguments, SW_NORMAL);
  38.   FProcessActive := False;
  39. end;
  40. function TWinExec.StartApp(AppName, ArgStr :String; Visibility : integer):integer;
  41. var
  42.   zAppName : FileSpec;
  43.   zCurDir : FileSpec;
  44.   WorkDir : String;
  45.   StartupInfo : TStartupInfo;
  46.   ProcessInfo : TProcessInformation;
  47. begin
  48.   if ArgStr <> '' then
  49.      AppName := AppName + ' ' + ArgStr;
  50.   StrPCopy(zAppName, AppName);
  51.   GetDir(0, WorkDir);
  52.   StrPCopy(zCurDir, WorkDir);
  53.   FillChar(StartupInfo, Sizeof(StartupInfo), #0);
  54.   StartupInfo.cb := Sizeof(StartupInfo);
  55.   StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
  56.   StartupInfo.wShowWindow := Visibility;
  57.   if not CreateProcess(
  58.     nil,                           { pointer to executable}
  59.     zAppName,                      { pointer to command line string }
  60.     nil,                           { pointer to process security attributes }
  61.     nil,                           { pointer to thread security attributes }
  62.     false,                         { handle inheritance flag }
  63.     CREATE_NEW_CONSOLE or          { creation flags }
  64.     NORMAL_PRIORITY_CLASS,
  65.     nil,                           { pointer to new environment block }
  66.     zCurDir,                       { pointer to current directory name }
  67.     StartupInfo,                   { pointer to STARTUPINFO }
  68.     ProcessInfo) then Result := -1 { pointer to PROCESS_INF }
  69.   else
  70.   begin
  71.     if Assigned(FOnProcessIdle) then
  72.     begin
  73.       WaitForInputIdle(ProcessInfo.dwProcessId, INFINITE);
  74.       FOnProcessIdle(Self);
  75.     end;
  76.     WaitforSingleObject(ProcessInfo.hProcess,INFINITE);
  77.     GetExitCodeProcess(ProcessInfo.hProcess,Result);
  78.     CloseHandle(ProcessInfo.hProcess );
  79.     CloseHandle(ProcessInfo.hThread );
  80.   end;
  81. end;
  82. end. // Winexec.pas
  83.  
  84. Here's a simple example that uses TWinExec.
  85.  
  86. unit Unit1;
  87. interface
  88. uses
  89.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  90.   StdCtrls, WinExec;
  91. type
  92.   TForm1 = class(TForm)
  93.     Edit1: TEdit;
  94.     Edit2: TEdit;
  95.     Label1: TLabel;
  96.     Label2: TLabel;
  97.     Button1: TButton;
  98.     Label3: TLabel;
  99.     procedure Button1Click(Sender: TObject);
  100.     procedure Edit1Change(Sender: TObject);
  101.   private
  102.     Process : TWinExec;
  103.     procedure ProcessDone(Sender: TObject);
  104.     procedure ProcessReady(Sender: TObject);
  105.   public
  106.   end;
  107. var
  108.   Form1: TForm1;
  109. implementation
  110. {$R *.DFM}
  111. procedure TForm1.Button1Click(Sender: TObject);
  112. begin
  113.    Process := TWinExec.Create(Edit1.Text, Edit2.Text, ProcessDone, ProcessReady);
  114.    Button1.Enabled := False;
  115.    Button1.Caption := 'Waiting for Launched Application to Finish';
  116.    Label3.Caption := 'Waiting for Idle Message';
  117. end;
  118. procedure TForm1.ProcessDone(Sender: TObject);
  119. begin
  120.   Process.Free;
  121.   Button1.Enabled := True;
  122.   Button1.Caption := 'Click to Start the Application';
  123. end;
  124. procedure TForm1.ProcessReady(Sender: TObject);
  125. begin
  126.   Label3.Caption := 'Application OnIdle received.';
  127. end;
  128. procedure TForm1.Edit1Change(Sender: TObject);
  129. begin
  130.   Button1.Enabled := FileExists(Edit1.Text);
  131. end;
  132. end.
  133. // =============  UNIT1.TXT ======================
  134. object Form1: TForm1
  135.   Left = 200
  136.   Top = 200
  137.   Width = 435
  138.   Height = 177
  139.   Caption = 'Form1'
  140.   Font.Color = clWindowText
  141.   Font.Height = -11
  142.   Font.Name = 'MS Sans Serif'
  143.   Font.Style = []
  144.   PixelsPerInch = 96
  145.   TextHeight = 13
  146.   object Label1: TLabel
  147.     Left = 24
  148.     Top = 16
  149.     Width = 53
  150.     Height = 13
  151.     Caption = 'Executable'
  152.   end
  153.   object Label2: TLabel
  154.     Left = 24
  155.     Top = 44
  156.     Width = 53
  157.     Height = 13
  158.     Caption = 'Arguments:'
  159.   end
  160.   object Label3: TLabel
  161.     Left = 26
  162.     Top = 76
  163.     Width = 17
  164.     Height = 13
  165.     Caption = 'Idle'
  166.   end
  167.   object Edit1: TEdit
  168.     Left = 83
  169.     Top = 12
  170.     Width = 309
  171.     Height = 21
  172.     TabOrder = 0
  173.     OnChange = Edit1Change
  174.   end
  175.   object Button1: TButton
  176.     Left = 20
  177.     Top = 104
  178.     Width = 373
  179.     Height = 25
  180.     Caption = 'Click to Start Application'
  181.     Enabled = False
  182.     TabOrder = 1
  183.     OnClick = Button1Click
  184.   end
  185.   object Edit2: TEdit
  186.     Left = 83
  187.     Top = 44
  188.     Width = 309
  189.     Height = 21
  190.     TabOrder = 2
  191.   end
  192. end
  193.